<?php

/**
 * Passenger form.
 *
 * @package    form
 * @subpackage Passenger
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 6174 2007-11-27 06:22:40Z fabien $
 */
class PassengerForm extends BasePassengerForm
{
  public function configure()
  {
	
		unset(
      $this['created_at'], $this['updated_at']
    );
    $this->widgetSchema['person_id'] = new sfWidgetFormInputHidden();
	
		$emergency_contactForm = new sfForm();
		if (!$this->isNew()) {  
	   // embed all old Phone forms
	   foreach ($this->getObject()->getEmergencyContact() as $emergency_contact) {
	   	$emergency_contactForm->embedForm('emergency_contact'.$emergency_contact->getId(), new EmergencyContactForm($emergency_contact));
	   }
		}
		//create blank phone form at end
		 $emergency_contactForm->embedForm('emergency_contact', new EmergencyContactForm());
		 $this->embedForm('emergency_contacts', $emergency_contactForm);
		
		
		
		$partner_familyForm = new sfForm();
		if (!$this->isNew()) {  
	   // embed all old Phone forms
	   foreach ($this->getObject()->getPartnerFamily() as $partner_family) {
	   	$partner_familyForm->embedForm('partner_family'.$partner_family->getId(), new PartnerFamilyForm($partner_family));
	   }
		}
		//create blank phone form at end
		 $partner_familyForm->embedForm('partner_family', new PartnerFamilyForm());
		 $this->embedForm('partner_familys', $partner_familyForm);
		
		
		$travel_mileageForm = new sfForm();
		if (!$this->isNew()) {  
	   // embed all old Phone forms
	   foreach ($this->getObject()->getTravelMileage() as $travel_mileage) {
	   	$travel_mileageForm->embedForm('travel_mileage'.$travel_mileage->getId(), new TravelMileageForm($travel_mileage));
	   }
		}
		//create blank phone form at end
		 $travel_mileageForm->embedForm('travel_mileage', new TravelMileageForm());
		 $this->embedForm('travel_mileages', $travel_mileageForm);
	
  }

	/**
	   * Update the Person_id of all the forms after the booking has been saved
	   */
	  public function saveEmbeddedForms($con = null, $forms = null)
	  {    
	    foreach($this->embeddedForms['emergency_contacts']->getEmbeddedForms() as $emergency_contactForm)
	    {
	      if (!$emergency_contactForm->getObject()->getPassengerId())
	      {
	        $emergency_contactForm->getObject()->setPassengerId($this->getObject()->getId());
					//$allForms->getObject()->setAuthor($this->getObject()->getAuthor()); setting of author. implement when already using sfGuard
	      }
	    }

			foreach($this->embeddedForms['partner_familys']->getEmbeddedForms() as $partner_familyForm)
	    {
	      if (!$partner_familyForm->getObject()->getPassengerId())
	      {
	        $partner_familyForm->getObject()->setPassengerId($this->getObject()->getId());
					//$allForms->getObject()->setAuthor($this->getObject()->getAuthor()); setting of author. implement when already using sfGuard
	      }
	    }

			foreach($this->embeddedForms['travel_mileages']->getEmbeddedForms() as $travel_mileageForm)
	    {
	      if (!$travel_mileageForm->getObject()->getPassengerId())
	      {
	        $travel_mileageForm->getObject()->setPassengerId($this->getObject()->getId());
					//$allForms->getObject()->setAuthor($this->getObject()->getAuthor()); setting of author. implement when already using sfGuard
	      }
	    }

	    parent::saveEmbeddedForms($con, $forms);
	  }



		public function bind(array $taintedValues = null, array $taintedFiles = null)
		{
			 if (is_null($taintedValues['emergency_contacts']['emergency_contact']['name']) || strlen($taintedValues['emergency_contacts']['emergency_contact']['name']) === 0 ) 
			  {
			    unset($this->embeddedForms['emergency_contacts']['emergency_contact']);
			    $this->validatorSchema['emergency_contacts']['emergency_contact'] = new sfValidatorPass();
			    $taintedValues['emergency_contacts']['emergency_contact'] = null;
			  }

			  if (is_null($taintedValues['partner_familys']['partner_family']['name'])|| strlen($taintedValues['partner_familys']['partner_family']['name']) === 0 )
			  {
			    unset($this->embeddedForms['partner_familys']['partner_family']);
			    $this->validatorSchema['partner_familys']['partner_family'] = new sfValidatorPass();
			    $taintedValues['partner_familys']['partner_family'] = null;
			  }

				if (is_null($taintedValues['travel_mileages']['travel_mileage']['id_number'])|| strlen($taintedValues['travel_mileages']['travel_mileage']['id_number']) === 0 )
			  {
			    unset($this->embeddedForms['travel_mileages']['travel_mileage']);
			    $this->validatorSchema['travel_mileages']['travel_mileage'] = new sfValidatorPass();
			    $taintedValues['travel_mileages']['travel_mileage'] = null;
			  }


			  parent::bind($taintedValues, $taintedFiles);
		}



}